package com.linsen.erp.admin.feign;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linsen.core.redis.util.RedisSecurityUtil;
import com.linsen.erp.admin.entity.UserEntity;
import com.linsen.erp.admin.provider.feign.IAdminFeignClient;
import com.linsen.erp.admin.provider.vo.SystemUserVO;
import com.linsen.erp.admin.service.IMenuService;
import com.linsen.erp.admin.service.IRoleService;
import com.linsen.erp.admin.service.IUserService;
import com.linsen.core.common.constant.ProviderConstant;
import com.linsen.core.common.enums.DeleteType;
import com.linsen.core.common.enums.UseStatusType;
import com.linsen.core.common.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @ProjectName: erp
 * @Package: com.linsen.erp.admin.feign
 * @ClassName: AdminFeignClient
 * @Author: zhaoyong
 * @Email: zy765203718@gmail.com
 * @Date: 2023/7/24 15:05
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class AdminFeignClient implements IAdminFeignClient {

    private final RedisSecurityUtil redisSecurityUtil;
    private final IUserService iUserService;
    private final IRoleService iRoleService;
    private final IMenuService iMenuService;


    /** 检查账号状态 */
    private Result loginCheck(UserEntity userEntity){
        if (ObjectUtil.isNull(userEntity)){
            return Result.error("账户或密码错误");
        }

        Integer status = userEntity.getStatus();
        if (ObjectUtil.equal(status, UseStatusType.UNABLE.getValue())){
            return Result.error("当前账户已被冻结，请联系系统管理员");
        }
        String userid = userEntity.getId();
        Integer isAdmin = userEntity.getIsAdmin();

        List<String> roleList = iRoleService.getCodeByUserid(userid,isAdmin);
        List<String> permissionList = CollectionUtil.newArrayList(iMenuService.getPermissionByUserid(userid,isAdmin));

        SystemUserVO systemUserVO = new SystemUserVO();
        systemUserVO.setUserid(userid);
        systemUserVO.setUsername(userEntity.getUsername());
        systemUserVO.setName(userEntity.getName());
        systemUserVO.setDeptId(userEntity.getDeptId());
        systemUserVO.setRoleList(roleList);
        systemUserVO.setPermissionList(permissionList);
        systemUserVO.setIsSuper(userEntity.getIsSuper());
        systemUserVO.setIsAdmin(isAdmin);

        iRoleService.setSystemUserVO(systemUserVO,userEntity);

        return Result.data(systemUserVO);
    }

    @Override
    @GetMapping(ProviderConstant.PROVIDER_LOGIN_CHECK_USERNAME)
    public Result loginCheckByUsername(String username, String password) {
        UserEntity userEntity = iUserService.getOne(
                new LambdaQueryWrapper<UserEntity>()
                        .eq(UserEntity::getUsername, username)
                        .eq(UserEntity::getPassword, password)
                        .eq(UserEntity::getIsDeleted, DeleteType.NORMAL.getValue())
        );
        return loginCheck(userEntity);
    }

    @Override
    @GetMapping(ProviderConstant.PROVIDER_LOGIN_CHECK_PHONE)
    public Result loginCheckByPhone(String phone) {
        UserEntity userEntity = iUserService.getOne(
                new LambdaQueryWrapper<UserEntity>()
                        .eq(UserEntity::getTel, phone)
                        .eq(UserEntity::getIsDeleted, DeleteType.NORMAL.getValue())
        );
        return loginCheck(userEntity);
    }
}
